---------------------------------------
--          AmBEffectsPanel          --
---------------------------------------
-- Panel that can smooth resizing and
-- showing/hiding.
local EffectsPanel = {
   Init = function(self)
		self._TargetWidth = 1
		self._TargetHeight = 1
		self._TargetAlpha = 255
		self._CurWidth = 1
		self._CurHeight = 1
		self._CurAlpha = 255
		
		self._MoveFactor = ThemeVar("default_effectspanel_move_speed")
		self._FadeFactor = ThemeVar("default_effectspanel_fade_factor")
		
		self._FadeOnHide = ThemeVar("default_effectspanel_should_fadeonhide")
		
		self._Shown = true
		self._Snapping = true -- If true, will listen to the next request to change size
   end,

   Paint = function(self)

   end,
	
	-- Sets the amount of pixels per seconds the
	-- effects panel resizes by.
	SetMoveFactor = function(self, PPS)
		self._MoveFactor = PPS
	end,
	
	-- Sets the fade factor in parts of 255 per second.
	SetFadeFactor = function(self, BPS)
		self._FadeFactor = BPS
	end,
	
	-- Sets the alpha to fade in/out to.
	SetTargetAlpha = function(self, Alpha)
		self._TargetAlpha = Alpha
	end,
	
	-- Gets the current alpha of the panel.
	GetCurrentAlpha = function(self)
		return self._CurAlpha
	end,
	
	-- Sets the current alpha of the panel.
	SetCurrentAlpha = function(self, Alpha)
		self._CurAlpha = Alpha
	end,

	-- Hides the panel.
	Hide = function(self)
		self._Shown = false
		self._TargetWidth = 0
		self._TargetHeight = 0
		
		if Freeze(self._FadeOnHide) then
			self._TargetAlpha = 0
		end
	end,
	
	-- Shows the panel.
	Show = function(self)
		self._Shown = true
		if Freeze(self._FadeOnHide) then
			self._TargetAlpha = 255
		end
	end,
	
   -- Gets the interior panel.
   GetInterior = function(self)
      return self
   end,
	
	OnChildRequestResize = function(self, NewWidth, NewHeight)
		if self._Shown then
			if self._Snapping then
				self._CurWidth = NewWidth
				self._CurHeight = NewHeight
				self._Snapping = false
			end
			self._TargetWidth = NewWidth
			self._TargetHeight = NewHeight
		end
	end,
	
	PanelAnimate = function(self, Time)
		local function smooth(cur, tar, factor)
			if cur == tar then
				return tar
			end
			if cur > tar then
				return math.max(tar, cur - factor)
			end
			if cur < tar then
				return math.min(tar, cur + factor)
			end
		end
		local mfactor = Freeze(self._MoveFactor)
		mfactor = mfactor * Time
		local ffactor = Freeze(self._FadeFactor)
		ffactor = ffactor * Time
		self._CurWidth = smooth(self._CurWidth, self._TargetWidth, mfactor)
		self._CurHeight = smooth(self._CurHeight, self._TargetHeight, mfactor)
		
		self._CurAlpha = smooth(self._CurAlpha, self._TargetAlpha, ffactor)
	end,

   PanelThink = function(self)
		if not self._Snapping then
			self:RequestResize(self._CurWidth, self._CurHeight)
		end
		self:SetAlpha(self._CurAlpha)
   end
}
AmB.UI.EffectsPanel = EffectsPanel
vgui.Register("AmBEffectsPanel", EffectsPanel, "AmBContentPanel")